Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Public Class FrmApprove
    Private Shared DES As New TripleDESCryptoServiceProvider()
    Private Shared MD5 As New MD5CryptoServiceProvider()

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spLogin", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@USERID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@PW", SqlDbType.Binary, 16)
        myCommand.Parameters("@USERID").Value = Trim(txtUserName.Text)
        myCommand.Parameters("@PW").Value = GenerateMD5_Pw(Trim(TxtPw.Text))

        Dim strSPOut As String
        strSPOut = myCommand.ExecuteScalar()
        If strSPOut <> "PASS" Then
            lblErrLog.Visible = True
            lblErrLog.Text = strSPOut
        Else
            lblErrLog.Visible = False
            TxtPw.Text = ""
            Approve_Event()
        End If

    End Sub
    Private Sub Approve_Event()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        'event
        myCommand = New SqlCommand("sp_LockUnlockEvent_ByUserLogin", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@Event_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@LockFlag", SqlDbType.VarChar)
        myCommand.Parameters("@Event_ID").Value = GV_SELECTED_EVENTID
        myCommand.Parameters("@EnteredBy").Value = Trim(txtUserName.Text)
        myCommand.Parameters("@LockFlag").Value = GV_APPROVAL_TYPE
        myCommand.ExecuteNonQuery()
        GV_APPROVED = True
        FrmDiagnosis.Close()
        FrmDiagnosis.Show()
        FrmDiagnosis.Focus()
    End Sub
    Function GenerateMD5_Pw(ByVal vb4EncryptPW) As Byte()
        Dim md5Hasher As New MD5CryptoServiceProvider()

        Dim hashedBytes As Byte()
        Dim encoder As New UTF8Encoding()

        hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(vb4EncryptPW))
        GenerateMD5_Pw = hashedBytes


    End Function
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Close()
    End Sub

    Private Sub FrmApprove_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        TxtPw.Focus()
    End Sub

    Private Sub FrmApprove_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtUserName.Text = GV_USERLOGINID
        TxtPw.Text = ""
        lblErrLog.Text = ""
    End Sub

    Private Sub TxtPw_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtPw.KeyDown
        If e.KeyValue = 13 Then
            BtnAdd_Click(sender, e)
        End If
    End Sub

    Private Sub TxtPw_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtPw.TextChanged

    End Sub
End Class